#!/bin/bash
#############################################################################
#  Copyright (C) 2013-2015 Lawrence Livermore National Security, LLC.
#  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
#  Written by Albert Chu <chu11@llnl.gov>
#  LLNL-CODE-644248
#
#  This file is part of Magpie, scripts for running Hadoop on
#  traditional HPC systems.  For details, see https://github.com/llnl/magpie.
#
#  Magpie is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  Magpie is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with Magpie.  If not, see <http://www.gnu.org/licenses/>.
#############################################################################

# These are functions to be called by magpie-run

source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-submission-type
source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-dirs
source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-user
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-log
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-paths

Magpie_run_start_pig () {
    if [ "${PIG_SETUP}" == "yes" ] && [ "${magpie_run_prior_startup_successful}" == "true" ]
    then
        if [ "${magpie_run_hadoop_setup_successful}" == "0" ]
        then
            Magpie_output_internal_warning "Attempt to setup Pig without Hadoop being setup"
        fi

        if [ "${PIG_OPTS}X" != "X" ]
        then
            if ! echo ${PIG_OPTS} | grep -q -E "java.io.tmpdir"
            then
                export PIG_OPTS="${PIG_OPTS} -Djava.io.tmpdir=${PIG_LOCAL_SCRATCHSPACE_DIR}/tmp"
            fi
        else
            export PIG_OPTS="-Djava.io.tmpdir=${PIG_LOCAL_SCRATCHSPACE_DIR}/tmp"
        fi
        echo "*******************************************************"
        echo "*"
        echo "* Pig Information"
        echo "*"
        echo "* To run Pig directly, follow above instructions for"
        echo "* to your allocation, then:"
        echo "*"
        if echo $MAGPIE_SHELL | grep -q csh
        then
            echo "*   setenv PIG_HOME \"${PIG_HOME}\""
            echo "*   setenv PIG_CONF_DIR \"${PIG_CONF_DIR}\""
            if [ "${PIG_OPTS}X" != "X" ]
            then
                echo "*   setenv PIG_OPTS \"${PIG_OPTS}\""
            fi
        else
            echo "*   export PIG_HOME=\"${PIG_HOME}\""
            echo "*   export PIG_CONF_DIR=\"${PIG_CONF_DIR}\""
            if [ "${PIG_OPTS}X" != "X" ]
            then
                echo "*   export PIG_OPTS=\"${PIG_OPTS}\""
            fi
        fi
        echo "*"
        echo "* Then you can do as you please.  For example to run a job:"
        echo "*"
        echo "*   \$PIG_HOME/${pigcmdprefix}/pig ..."
        echo "*"
        if [ "${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}X" != "X" ]
        then
            echo "* If running interactively, sourcing"
            echo "*"
            echo "* ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}"
            echo "*"
            echo "* will set most common environment variables for your job."
            echo "*"
        fi
        echo "*******************************************************"

        if [ "${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}X" != "X" ]
        then
            if echo $MAGPIE_SHELL | grep -q csh
            then
                echo "setenv PIG_HOME \"${PIG_HOME}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv PIG_CONF_DIR \"${PIG_CONF_DIR}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                if [ "${PIG_OPTS}X" != "X" ]
                then
                    echo "setenv PIG_OPTS \"${PIG_OPTS}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                fi
            else
                echo "export PIG_HOME=\"${PIG_HOME}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export PIG_CONF_DIR=\"${PIG_CONF_DIR}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                if [ "${PIG_OPTS}X" != "X" ]
                then
                    echo "export PIG_OPTS=\"${PIG_OPTS}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                fi
            fi
            echo "" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
        fi
    fi
}

Magpie_run_pig () {
    if [ "${PIG_JOB}" == "testpig" ]
    then
        echo "*******************************************************"
        echo "* Running Testpig"
        echo "*******************************************************"
        ${MAGPIE_SCRIPTS_HOME}/magpie/run/magpie-run-execute script ${MAGPIE_SCRIPTS_HOME}/magpie/job/magpie-job-pig-testpig &
        local scriptpid=$!
        Magpie_wait_script_sigusr2_on_job_timeout ${scriptpid}
    elif [ "${PIG_JOB}" == "script" ]
    then
        echo "*******************************************************"
        echo "* Executing Pig script $PIG_SCRIPT_PATH"
        echo "*******************************************************"
        ${MAGPIE_SCRIPTS_HOME}/magpie/run/magpie-run-execute script "${PIG_HOME}/${pigcmdprefix}/pig ${PIG_SCRIPT_OPTS} ${PIG_SCRIPT_PATH}" &
        local scriptpid=$!
        Magpie_wait_script_sigusr2_on_job_timeout ${scriptpid}
    else
        Magpie_output_internal_error "PIG_JOB = ${PIG_JOB} not handled"
    fi
}